2 Dimensional Array Manipulations and Equalization

I have absolutely no idea why the below errors occur, I searched through stack exchange and apparently there is some kind of circular logic occuring with the matplotlib import, but my error was not fixed through python kernel editing unfortunately.


In [5]:
import matplotlib.pyplot as plt
import scipy.ndimage

import csv,gc
import matplotlib
import numpy as np
import nibabel as nb

%matplotlib inline
BINS = 32


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-3b917940efc5> in <module>()
----> 1 import matplotlib.pyplot as plt
      2 import scipy.ndimage
      3 
      4 import csv,gc
      5 import matplotlib

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py in <module>()
    112 
    113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    115 
    116 _IP_REGISTERED = None

/usr/lib/python2.7/dist-packages/matplotlib/backends/__init__.pyc in pylab_setup()
     30     # imports. 0 means only perform absolute imports.
     31     backend_mod = __import__(backend_name,
---> 32                              globals(),locals(),[backend_name],0)
     33 
     34     # Things we pull in from all backends

/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in <module>()
    154         configure_inline_support(ip, backend)
    155 
--> 156 _enable_matplotlib_integration()

/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in _enable_matplotlib_integration()
    152     backend = get_backend()
    153     if ip and backend == 'module://%s' % __name__:
--> 154         configure_inline_support(ip, backend)
    155 
    156 _enable_matplotlib_integration()

/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in configure_inline_support(shell, backend)
    359     except ImportError:
    360         return
--> 361     from matplotlib import pyplot
    362 
    363     cfg = InlineBackend.instance(parent=shell)

ImportError: cannot import name pyplot

In [6]:
import csv,gc
import matplotlib
import numpy as np
import nibabel as nb

%matplotlib inline
BINS = 32


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-0c0e6f548e62> in <module>()
      4 import nibabel as nb
      5 
----> 6 get_ipython().magic(u'matplotlib inline')
      7 BINS = 32

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2161         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2162         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2163         return self.run_line_magic(magic_name, magic_arg_s)
   2164 
   2165     #-------------------------------------------------------------------------

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2082                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2083             with self.builtin_trap:
-> 2084                 result = fn(*args,**kwargs)
   2085             return result
   2086 

<decorator-gen-106> in matplotlib(self, line)

/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/usr/local/lib/python2.7/dist-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line)
     98             print("Available matplotlib backends: %s" % backends_list)
     99         else:
--> 100             gui, backend = self.shell.enable_matplotlib(args.gui)
    101             self._show_matplotlib_backend(args.gui, backend)
    102 

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in enable_matplotlib(self, gui)
   2949                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   2950 
-> 2951         pt.activate_matplotlib(backend)
   2952         pt.configure_inline_support(self, backend)
   2953 

/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in activate_matplotlib(backend)
    293     matplotlib.rcParams['backend'] = backend
    294 
--> 295     import matplotlib.pyplot
    296     matplotlib.pyplot.switch_backend(backend)
    297 

/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py in <module>()
    112 
    113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    115 
    116 _IP_REGISTERED = None

/usr/lib/python2.7/dist-packages/matplotlib/backends/__init__.pyc in pylab_setup()
     30     # imports. 0 means only perform absolute imports.
     31     backend_mod = __import__(backend_name,
---> 32                              globals(),locals(),[backend_name],0)
     33 
     34     # Things we pull in from all backends

/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in <module>()
    154         configure_inline_support(ip, backend)
    155 
--> 156 _enable_matplotlib_integration()

/usr/local/lib/python2.7/dist-packages/ipykernel/pylab/backend_inline.py in _enable_matplotlib_integration()
    152     backend = get_backend()
    153     if ip and backend == 'module://%s' % __name__:
--> 154         configure_inline_support(ip, backend)
    155 
    156 _enable_matplotlib_integration()

/usr/local/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in configure_inline_support(shell, backend)
    359     except ImportError:
    360         return
--> 361     from matplotlib import pyplot
    362 
    363     cfg = InlineBackend.instance(parent=shell)

ImportError: cannot import name pyplot

In [1]:
### Run below if necessary

##import sys

##sys.path.append('/usr/local/lib/python2.7/site-packages')

import math
import csv,gc
import matplotlib
import numpy as np
import cv2

#%matplotlib
BINS = 32

In [2]:
import matplotlib.pyplot as plt
%matplotlib inline

In [3]:
from skimage import data, img_as_float
from skimage import exposure

In [12]:
z = np.random.randint(0.0,10.0,(2,2))
print z


[[3 6]
 [3 0]]

In [13]:
print z[0]
print z[1]


[3 6]
[3 0]

In [57]:
zz = z.reshape(-1)
print zz
print zz.sum()


[3 6 3 0]
12

In [58]:
plt.hist(zz, bins='auto')
plt.show()

## We expect 1 zero, 2 threes, 1 six



In [59]:
plt.hist(zz, bins = 10)
plt.show()



In [102]:
## Histogram Normalization
'''zhisteq = zz
zz.astype(float)
i=0
while i<7:
    zhisteq[i] = (zz[i]/histsum)
    i+=1
zhisteq.astype(float)
print zhisteq '''

zhistnorm = zz*1.0/sum(zz)
print zhistnorm

plt.hist(zhistnorm, bins = 7)
plt.show


[ 0.25  0.5   0.25  0.  ]
Out[102]:
<function matplotlib.pyplot.show>

Let's pretend Z is a very simple image


In [93]:
import math

## Reminder of Z's values
print z

# Number of pixels
pixels = len(z) * len(z[0])
print "The number of pixels is {}".format(pixels)
## The output should be 4 since there are 4 numbers in this array

##Flatten method doesn't work because of tuple conversion later on
'''# Now we flatten Z
zz = z.reshape(-1)'''


# Initialize histogram and CDF
hist = {}
cdf = {}
norm_cdf = {}
## The range value should be be adjusted to the bin number
### In our case the range and bin number is obviously 10 
### since we generated numbers 1-10
BINS = 10

for i in range(BINS):
    hist[i] = 0
    cdf[i] = 0
    norm_cdf[i] = 0

# Create histogram


for row in z:
    for val in row:
        hist[val] += 1

'''
for val in zz:
    hist[val] += 1
'''    


# Create cdf
for i in range(BINS):
    for j in range(i+1):
        cdf[i] += hist[j]
    norm_cdf[i] = int(math.floor(float(cdf[i]-1)/63*BINS))
    
print "The histogram values are {}".format(hist)
print "The cdf values are {}".format(cdf)
print "The normalized cdf values are {}".format(norm_cdf)


[[3 6]
 [3 0]]
The number of pixels is 4
The histogram values are {0: 1, 1: 0, 2: 0, 3: 2, 4: 0, 5: 0, 6: 1, 7: 0, 8: 0, 9: 0}
The cdf values are {0: 1, 1: 1, 2: 1, 3: 3, 4: 3, 5: 3, 6: 4, 7: 4, 8: 4, 9: 4}
The normalized cdf values are {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}

This matches our constructed histogram so we can proceed to create a new histogram and construct the equalized one


In [94]:
newimg = np.empty(z.shape)
## This should be the same exact dimensions as the original array
print newimg 
print z.shape

## set x_length to the first number, y_length to the second
x_length = z.shape[0]
y_length = z.shape[1]

print x_length, y_length


[[ 0.  0.]
 [ 0.  0.]]
(2, 2)
2 2

In [95]:
for i in range(x_length):
    for j in range(y_length):
        newimg[i][j] = norm_cdf[ z[i][j] ]

print newimg


[[ 0.  0.]
 [ 0.  0.]]

This probably didn't work because of the tiny size of the array, let's try something more established


In [5]:
img = [
    [52, 55, 61,  66,  70,  61, 64, 73],
    [63, 59, 55,  90, 109,  85, 69, 72],
    [62, 59, 68, 113, 144, 104, 66, 73],
    [63, 58, 71, 122, 154, 106, 70, 69],
    [67, 61, 68, 104, 126,  88, 68, 70],
    [79, 65, 60,  70,  77,  68, 58, 75],
    [85, 71, 64,  59,  55,  61, 65, 83],
    [87, 79, 69,  68,  65,  76, 78, 94]
]

img = np.asarray(img)
print img

print " "

print img[0]
print img[1]

print " "

imgflat = img.reshape(-1)
print imgflat
print imgflat.sum()

print " "
fig = plt.hist(imgflat, bins='auto')
plt.title('Histogram')
plt.show()

print " "

imgnorm = imgflat*1.0/sum(imgflat)
print imgnorm

fig = plt.hist(imgnorm, bins = 'auto')
plt.title('Normalized Histogram')
plt.show


[[ 52  55  61  66  70  61  64  73]
 [ 63  59  55  90 109  85  69  72]
 [ 62  59  68 113 144 104  66  73]
 [ 63  58  71 122 154 106  70  69]
 [ 67  61  68 104 126  88  68  70]
 [ 79  65  60  70  77  68  58  75]
 [ 85  71  64  59  55  61  65  83]
 [ 87  79  69  68  65  76  78  94]]
 
[52 55 61 66 70 61 64 73]
[ 63  59  55  90 109  85  69  72]
 
[ 52  55  61  66  70  61  64  73  63  59  55  90 109  85  69  72  62  59
  68 113 144 104  66  73  63  58  71 122 154 106  70  69  67  61  68 104
 126  88  68  70  79  65  60  70  77  68  58  75  85  71  64  59  55  61
  65  83  87  79  69  68  65  76  78  94]
4869
 
 
[ 0.01067981  0.01129595  0.01252824  0.01355514  0.01437667  0.01252824
  0.01314438  0.01499281  0.012939    0.01211748  0.01129595  0.01848429
  0.02238653  0.01745738  0.01417129  0.01478743  0.01273362  0.01211748
  0.01396591  0.02320805  0.02957486  0.02135962  0.01355514  0.01499281
  0.012939    0.0119121   0.01458205  0.02505648  0.03162867  0.02177038
  0.01437667  0.01417129  0.01376053  0.01252824  0.01396591  0.02135962
  0.025878    0.01807353  0.01396591  0.01437667  0.0162251   0.01334976
  0.01232286  0.01437667  0.01581434  0.01396591  0.0119121   0.01540357
  0.01745738  0.01458205  0.01314438  0.01211748  0.01129595  0.01252824
  0.01334976  0.01704662  0.01786815  0.0162251   0.01417129  0.01396591
  0.01334976  0.01560895  0.01601972  0.01930581]
Out[5]:
<function matplotlib.pyplot.show>

In [6]:
import math

## Reminder of Z's values
print img

# Number of pixels
pixels = len(img) * len(img[0])
print "The number of pixels is {}".format(pixels)
## The output should be 4 since there are 4 numbers in this array

##Flatten method doesn't work because of tuple conversion later on
'''# Now we flatten Z
zz = z.reshape(-1)'''


# Initialize histogram and CDF
hist = {}
cdf = {}
norm_cdf = {}
## The range value should be be adjusted to the bin number

BINS = 255

for i in range(BINS):
    hist[i] = 0
    cdf[i] = 0
    norm_cdf[i] = 0

# Create histogram


for row in img:
    for val in row:
        hist[val] += 1

'''
for val in zz:
    hist[val] += 1
'''    


# Create cdf
for i in range(BINS):
    for j in range(i+1):
        cdf[i] += hist[j]
    norm_cdf[i] = int(math.floor(float(cdf[i]-1)/63*BINS))
    
print "The histogram values are {}".format(hist)
print "The cdf values are {}".format(cdf)
print "The normalized cdf values are {}".format(norm_cdf)


[[ 52  55  61  66  70  61  64  73]
 [ 63  59  55  90 109  85  69  72]
 [ 62  59  68 113 144 104  66  73]
 [ 63  58  71 122 154 106  70  69]
 [ 67  61  68 104 126  88  68  70]
 [ 79  65  60  70  77  68  58  75]
 [ 85  71  64  59  55  61  65  83]
 [ 87  79  69  68  65  76  78  94]]
The number of pixels is 64
The histogram values are {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0, 40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0, 50: 0, 51: 0, 52: 1, 53: 0, 54: 0, 55: 3, 56: 0, 57: 0, 58: 2, 59: 3, 60: 1, 61: 4, 62: 1, 63: 2, 64: 2, 65: 3, 66: 2, 67: 1, 68: 5, 69: 3, 70: 4, 71: 2, 72: 1, 73: 2, 74: 0, 75: 1, 76: 1, 77: 1, 78: 1, 79: 2, 80: 0, 81: 0, 82: 0, 83: 1, 84: 0, 85: 2, 86: 0, 87: 1, 88: 1, 89: 0, 90: 1, 91: 0, 92: 0, 93: 0, 94: 1, 95: 0, 96: 0, 97: 0, 98: 0, 99: 0, 100: 0, 101: 0, 102: 0, 103: 0, 104: 2, 105: 0, 106: 1, 107: 0, 108: 0, 109: 1, 110: 0, 111: 0, 112: 0, 113: 1, 114: 0, 115: 0, 116: 0, 117: 0, 118: 0, 119: 0, 120: 0, 121: 0, 122: 1, 123: 0, 124: 0, 125: 0, 126: 1, 127: 0, 128: 0, 129: 0, 130: 0, 131: 0, 132: 0, 133: 0, 134: 0, 135: 0, 136: 0, 137: 0, 138: 0, 139: 0, 140: 0, 141: 0, 142: 0, 143: 0, 144: 1, 145: 0, 146: 0, 147: 0, 148: 0, 149: 0, 150: 0, 151: 0, 152: 0, 153: 0, 154: 1, 155: 0, 156: 0, 157: 0, 158: 0, 159: 0, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 165: 0, 166: 0, 167: 0, 168: 0, 169: 0, 170: 0, 171: 0, 172: 0, 173: 0, 174: 0, 175: 0, 176: 0, 177: 0, 178: 0, 179: 0, 180: 0, 181: 0, 182: 0, 183: 0, 184: 0, 185: 0, 186: 0, 187: 0, 188: 0, 189: 0, 190: 0, 191: 0, 192: 0, 193: 0, 194: 0, 195: 0, 196: 0, 197: 0, 198: 0, 199: 0, 200: 0, 201: 0, 202: 0, 203: 0, 204: 0, 205: 0, 206: 0, 207: 0, 208: 0, 209: 0, 210: 0, 211: 0, 212: 0, 213: 0, 214: 0, 215: 0, 216: 0, 217: 0, 218: 0, 219: 0, 220: 0, 221: 0, 222: 0, 223: 0, 224: 0, 225: 0, 226: 0, 227: 0, 228: 0, 229: 0, 230: 0, 231: 0, 232: 0, 233: 0, 234: 0, 235: 0, 236: 0, 237: 0, 238: 0, 239: 0, 240: 0, 241: 0, 242: 0, 243: 0, 244: 0, 245: 0, 246: 0, 247: 0, 248: 0, 249: 0, 250: 0, 251: 0, 252: 0, 253: 0, 254: 0}
The cdf values are {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0, 40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0, 50: 0, 51: 0, 52: 1, 53: 1, 54: 1, 55: 4, 56: 4, 57: 4, 58: 6, 59: 9, 60: 10, 61: 14, 62: 15, 63: 17, 64: 19, 65: 22, 66: 24, 67: 25, 68: 30, 69: 33, 70: 37, 71: 39, 72: 40, 73: 42, 74: 42, 75: 43, 76: 44, 77: 45, 78: 46, 79: 48, 80: 48, 81: 48, 82: 48, 83: 49, 84: 49, 85: 51, 86: 51, 87: 52, 88: 53, 89: 53, 90: 54, 91: 54, 92: 54, 93: 54, 94: 55, 95: 55, 96: 55, 97: 55, 98: 55, 99: 55, 100: 55, 101: 55, 102: 55, 103: 55, 104: 57, 105: 57, 106: 58, 107: 58, 108: 58, 109: 59, 110: 59, 111: 59, 112: 59, 113: 60, 114: 60, 115: 60, 116: 60, 117: 60, 118: 60, 119: 60, 120: 60, 121: 60, 122: 61, 123: 61, 124: 61, 125: 61, 126: 62, 127: 62, 128: 62, 129: 62, 130: 62, 131: 62, 132: 62, 133: 62, 134: 62, 135: 62, 136: 62, 137: 62, 138: 62, 139: 62, 140: 62, 141: 62, 142: 62, 143: 62, 144: 63, 145: 63, 146: 63, 147: 63, 148: 63, 149: 63, 150: 63, 151: 63, 152: 63, 153: 63, 154: 64, 155: 64, 156: 64, 157: 64, 158: 64, 159: 64, 160: 64, 161: 64, 162: 64, 163: 64, 164: 64, 165: 64, 166: 64, 167: 64, 168: 64, 169: 64, 170: 64, 171: 64, 172: 64, 173: 64, 174: 64, 175: 64, 176: 64, 177: 64, 178: 64, 179: 64, 180: 64, 181: 64, 182: 64, 183: 64, 184: 64, 185: 64, 186: 64, 187: 64, 188: 64, 189: 64, 190: 64, 191: 64, 192: 64, 193: 64, 194: 64, 195: 64, 196: 64, 197: 64, 198: 64, 199: 64, 200: 64, 201: 64, 202: 64, 203: 64, 204: 64, 205: 64, 206: 64, 207: 64, 208: 64, 209: 64, 210: 64, 211: 64, 212: 64, 213: 64, 214: 64, 215: 64, 216: 64, 217: 64, 218: 64, 219: 64, 220: 64, 221: 64, 222: 64, 223: 64, 224: 64, 225: 64, 226: 64, 227: 64, 228: 64, 229: 64, 230: 64, 231: 64, 232: 64, 233: 64, 234: 64, 235: 64, 236: 64, 237: 64, 238: 64, 239: 64, 240: 64, 241: 64, 242: 64, 243: 64, 244: 64, 245: 64, 246: 64, 247: 64, 248: 64, 249: 64, 250: 64, 251: 64, 252: 64, 253: 64, 254: 64}
The normalized cdf values are {0: -5, 1: -5, 2: -5, 3: -5, 4: -5, 5: -5, 6: -5, 7: -5, 8: -5, 9: -5, 10: -5, 11: -5, 12: -5, 13: -5, 14: -5, 15: -5, 16: -5, 17: -5, 18: -5, 19: -5, 20: -5, 21: -5, 22: -5, 23: -5, 24: -5, 25: -5, 26: -5, 27: -5, 28: -5, 29: -5, 30: -5, 31: -5, 32: -5, 33: -5, 34: -5, 35: -5, 36: -5, 37: -5, 38: -5, 39: -5, 40: -5, 41: -5, 42: -5, 43: -5, 44: -5, 45: -5, 46: -5, 47: -5, 48: -5, 49: -5, 50: -5, 51: -5, 52: 0, 53: 0, 54: 0, 55: 12, 56: 12, 57: 12, 58: 20, 59: 32, 60: 36, 61: 52, 62: 56, 63: 64, 64: 72, 65: 85, 66: 93, 67: 97, 68: 117, 69: 129, 70: 145, 71: 153, 72: 157, 73: 165, 74: 165, 75: 170, 76: 174, 77: 178, 78: 182, 79: 190, 80: 190, 81: 190, 82: 190, 83: 194, 84: 194, 85: 202, 86: 202, 87: 206, 88: 210, 89: 210, 90: 214, 91: 214, 92: 214, 93: 214, 94: 218, 95: 218, 96: 218, 97: 218, 98: 218, 99: 218, 100: 218, 101: 218, 102: 218, 103: 218, 104: 226, 105: 226, 106: 230, 107: 230, 108: 230, 109: 234, 110: 234, 111: 234, 112: 234, 113: 238, 114: 238, 115: 238, 116: 238, 117: 238, 118: 238, 119: 238, 120: 238, 121: 238, 122: 242, 123: 242, 124: 242, 125: 242, 126: 246, 127: 246, 128: 246, 129: 246, 130: 246, 131: 246, 132: 246, 133: 246, 134: 246, 135: 246, 136: 246, 137: 246, 138: 246, 139: 246, 140: 246, 141: 246, 142: 246, 143: 246, 144: 250, 145: 250, 146: 250, 147: 250, 148: 250, 149: 250, 150: 250, 151: 250, 152: 250, 153: 250, 154: 255, 155: 255, 156: 255, 157: 255, 158: 255, 159: 255, 160: 255, 161: 255, 162: 255, 163: 255, 164: 255, 165: 255, 166: 255, 167: 255, 168: 255, 169: 255, 170: 255, 171: 255, 172: 255, 173: 255, 174: 255, 175: 255, 176: 255, 177: 255, 178: 255, 179: 255, 180: 255, 181: 255, 182: 255, 183: 255, 184: 255, 185: 255, 186: 255, 187: 255, 188: 255, 189: 255, 190: 255, 191: 255, 192: 255, 193: 255, 194: 255, 195: 255, 196: 255, 197: 255, 198: 255, 199: 255, 200: 255, 201: 255, 202: 255, 203: 255, 204: 255, 205: 255, 206: 255, 207: 255, 208: 255, 209: 255, 210: 255, 211: 255, 212: 255, 213: 255, 214: 255, 215: 255, 216: 255, 217: 255, 218: 255, 219: 255, 220: 255, 221: 255, 222: 255, 223: 255, 224: 255, 225: 255, 226: 255, 227: 255, 228: 255, 229: 255, 230: 255, 231: 255, 232: 255, 233: 255, 234: 255, 235: 255, 236: 255, 237: 255, 238: 255, 239: 255, 240: 255, 241: 255, 242: 255, 243: 255, 244: 255, 245: 255, 246: 255, 247: 255, 248: 255, 249: 255, 250: 255, 251: 255, 252: 255, 253: 255, 254: 255}

In [8]:
newimg = np.empty(img.shape)
## This should be the same exact dimensions as the original array
print newimg 
print img.shape

## set x_length to the first number, y_length to the second
x_length = img.shape[0]
y_length = img.shape[1]

print x_length, y_length

for i in range(x_length):
    for j in range(y_length):
        newimg[i][j] = norm_cdf[ img[i][j] ]

print newimg
fig = plt.hist(newimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show

## This is wrong


[[ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]]
(8, 8)
8 8
[[   0.   12.   52.   93.  145.   52.   72.  165.]
 [  64.   32.   12.  214.  234.  202.  129.  157.]
 [  56.   32.  117.  238.  250.  226.   93.  165.]
 [  64.   20.  153.  242.  255.  230.  145.  129.]
 [  97.   52.  117.  226.  246.  210.  117.  145.]
 [ 190.   85.   36.  145.  178.  117.   20.  170.]
 [ 202.  153.   72.   32.   12.   52.   85.  194.]
 [ 206.  190.  129.  117.   85.  174.  182.  218.]]
Out[8]:
<function matplotlib.pyplot.show>

In [12]:
for i in range(8):
    for j in range(8):
        newimg[i][j] = norm_cdf[ img[i][j] ]

print '+-------+-----------+-----+----------------+'
print '| %5s | %9s | %3s | %14s |' % ('Value', 'Histogram', 'cdf', 'Normalized cdf')
print '+-------+-----------+-----+----------------+'
for i in range(255):
    if hist[i] == 0: continue
    print '| %5s | %9s | %3s | %14s |' % (i, hist[i], cdf[i], norm_cdf[i])
print '+-------+-----------+-----+----------------+'

print ''
print 'Original subimage:'
print ''
for i in range(8):
    print ('%4d'*8) % tuple(img[i])

print ''
print ''
print 'Equalized subimage:'
print ''

for i in range(8):
    print ('%4d'*8) % tuple(newimg[i])


+-------+-----------+-----+----------------+
| Value | Histogram | cdf | Normalized cdf |
+-------+-----------+-----+----------------+
|    52 |         1 |   1 |              0 |
|    55 |         3 |   4 |             12 |
|    58 |         2 |   6 |             20 |
|    59 |         3 |   9 |             32 |
|    60 |         1 |  10 |             36 |
|    61 |         4 |  14 |             52 |
|    62 |         1 |  15 |             56 |
|    63 |         2 |  17 |             64 |
|    64 |         2 |  19 |             72 |
|    65 |         3 |  22 |             85 |
|    66 |         2 |  24 |             93 |
|    67 |         1 |  25 |             97 |
|    68 |         5 |  30 |            117 |
|    69 |         3 |  33 |            129 |
|    70 |         4 |  37 |            145 |
|    71 |         2 |  39 |            153 |
|    72 |         1 |  40 |            157 |
|    73 |         2 |  42 |            165 |
|    75 |         1 |  43 |            170 |
|    76 |         1 |  44 |            174 |
|    77 |         1 |  45 |            178 |
|    78 |         1 |  46 |            182 |
|    79 |         2 |  48 |            190 |
|    83 |         1 |  49 |            194 |
|    85 |         2 |  51 |            202 |
|    87 |         1 |  52 |            206 |
|    88 |         1 |  53 |            210 |
|    90 |         1 |  54 |            214 |
|    94 |         1 |  55 |            218 |
|   104 |         2 |  57 |            226 |
|   106 |         1 |  58 |            230 |
|   109 |         1 |  59 |            234 |
|   113 |         1 |  60 |            238 |
|   122 |         1 |  61 |            242 |
|   126 |         1 |  62 |            246 |
|   144 |         1 |  63 |            250 |
|   154 |         1 |  64 |            255 |
+-------+-----------+-----+----------------+

Original subimage:

  52  55  61  66  70  61  64  73
  63  59  55  90 109  85  69  72
  62  59  68 113 144 104  66  73
  63  58  71 122 154 106  70  69
  67  61  68 104 126  88  68  70
  79  65  60  70  77  68  58  75
  85  71  64  59  55  61  65  83
  87  79  69  68  65  76  78  94


Equalized subimage:

   0  12  52  93 145  52  72 165
  64  32  12 214 234 202 129 157
  56  32 117 238 250 226  93 165
  64  20 153 242 255 230 145 129
  97  52 117 226 246 210 117 145
 190  85  36 145 178 117  20 170
 202 153  72  32  12  52  85 194
 206 190 129 117  85 174 182 218

In [13]:
for i in range(x_length):
    for j in range(y_length):
        newimg[i][j] = norm_cdf[ img[i][j] ]

print newimg
fig = plt.hist(newimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show

histeqimg = np.empty(img.shape)
for i in range(8):
    histeqimg[i] = ('%4d'*8) % tuple(newimg[i])
    
    
print histeqimg
fig = plt.hist(histeqimg, bins = 'auto')
plt.title('Equalized Histogram 2')
plt.show


[[   0.   12.   52.   93.  145.   52.   72.  165.]
 [  64.   32.   12.  214.  234.  202.  129.  157.]
 [  56.   32.  117.  238.  250.  226.   93.  165.]
 [  64.   20.  153.  242.  255.  230.  145.  129.]
 [  97.   52.  117.  226.  246.  210.  117.  145.]
 [ 190.   85.   36.  145.  178.  117.   20.  170.]
 [ 202.  153.   72.   32.   12.   52.   85.  194.]
 [ 206.  190.  129.  117.   85.  174.  182.  218.]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-8b2a4d7927e7> in <module>()
     10 histeqimg = np.empty(img.shape)
     11 for i in range(8):
---> 12     histeqimg[i] = ('%4d'*8) % tuple(newimg[i])
     13 
     14 print histeqimg

ValueError: invalid literal for float(): 0  12  52  93 145  52  72 165

In [17]:
print(repr(histeqimg))
histeqimg.append(line.strip('\n').strip('\t').split(' ').pop(7))  
### Errors caused by massive number of zeros?


array([[  9.76118064e-313,   2.37151510e-322,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  9.76118064e-313,   2.37151510e-322,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  9.76118064e-313,   2.37151510e-322,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  9.76118064e-313,   2.37151510e-322,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000]])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-ad3c7c0c9483> in <module>()
      1 print(repr(histeqimg))
----> 2 histeqimg.append(line.strip('\n').strip('\t').split(' ').pop(7))

AttributeError: 'numpy.ndarray' object has no attribute 'append'

In [35]:
print ''
print 'Original subimage:'
print ''
for i in range(8):
    print ('%4d'*8) % tuple(img[i])
print ''
imgflat = img.reshape(-1)
print img

print " "
fig = plt.hist(imgflat, bins='auto')
plt.title('Original Histogram')
plt.show()

print ''
print ''
print 'Equalized subimage:'
print ''

for i in range(8):
    print ('%4d'*8) % tuple(newimg[i])

for i in range(x_length):
    for j in range(y_length):
        newimg[i][j] = norm_cdf[ img[i][j] ]
print ''
print newimg
fig = plt.hist(newimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show


Original subimage:

  52  55  61  66  70  61  64  73
  63  59  55  90 109  85  69  72
  62  59  68 113 144 104  66  73
  63  58  71 122 154 106  70  69
  67  61  68 104 126  88  68  70
  79  65  60  70  77  68  58  75
  85  71  64  59  55  61  65  83
  87  79  69  68  65  76  78  94

[[ 52  55  61  66  70  61  64  73]
 [ 63  59  55  90 109  85  69  72]
 [ 62  59  68 113 144 104  66  73]
 [ 63  58  71 122 154 106  70  69]
 [ 67  61  68 104 126  88  68  70]
 [ 79  65  60  70  77  68  58  75]
 [ 85  71  64  59  55  61  65  83]
 [ 87  79  69  68  65  76  78  94]]
 

Equalized subimage:

   0  12  52  93 145  52  72 165
  64  32  12 214 234 202 129 157
  56  32 117 238 250 226  93 165
  64  20 153 242 255 230 145 129
  97  52 117 226 246 210 117 145
 190  85  36 145 178 117  20 170
 202 153  72  32  12  52  85 194
 206 190 129 117  85 174 182 218

[[   0.   12.   52.   93.  145.   52.   72.  165.]
 [  64.   32.   12.  214.  234.  202.  129.  157.]
 [  56.   32.  117.  238.  250.  226.   93.  165.]
 [  64.   20.  153.  242.  255.  230.  145.  129.]
 [  97.   52.  117.  226.  246.  210.  117.  145.]
 [ 190.   85.   36.  145.  178.  117.   20.  170.]
 [ 202.  153.   72.   32.   12.   52.   85.  194.]
 [ 206.  190.  129.  117.   85.  174.  182.  218.]]
Out[35]:
<function matplotlib.pyplot.show>

In [47]:
fig = plt.hist(imgflat, bins=255)
plt.title('Original Histogram')
plt.show()



In [48]:
fig = plt.hist(newimg, bins = 255)
plt.title('Equalized Histogram')
plt.show


Out[48]:
<function matplotlib.pyplot.show>

In [40]:
print img
print ''
print newimg
print ''

flatimg = img.reshape(-1)
flattenedimg, bin_edges1 = np.histogram(flatimg)
print flatimg
print flattenedimg
print ''

flatnewimg = newimg.reshape(-1)
flattenednewimg, bin_edges2 = np.histogram(flatnewimg)
print flatnewimg
print flattenednewimg
print ''


[[ 52  55  61  66  70  61  64  73]
 [ 63  59  55  90 109  85  69  72]
 [ 62  59  68 113 144 104  66  73]
 [ 63  58  71 122 154 106  70  69]
 [ 67  61  68 104 126  88  68  70]
 [ 79  65  60  70  77  68  58  75]
 [ 85  71  64  59  55  61  65  83]
 [ 87  79  69  68  65  76  78  94]]

[[   0.   12.   52.   93.  145.   52.   72.  165.]
 [  64.   32.   12.  214.  234.  202.  129.  157.]
 [  56.   32.  117.  238.  250.  226.   93.  165.]
 [  64.   20.  153.  242.  255.  230.  145.  129.]
 [  97.   52.  117.  226.  246.  210.  117.  145.]
 [ 190.   85.   36.  145.  178.  117.   20.  170.]
 [ 202.  153.   72.   32.   12.   52.   85.  194.]
 [ 206.  190.  129.  117.   85.  174.  182.  218.]]

[ 52  55  61  66  70  61  64  73  63  59  55  90 109  85  69  72  62  59
  68 113 144 104  66  73  63  58  71 122 154 106  70  69  67  61  68 104
 126  88  68  70  79  65  60  70  77  68  58  75  85  71  64  59  55  61
  65  83  87  79  69  68  65  76  78  94]
[15 25  8  6  1  5  1  1  0  2]

[   0.   12.   52.   93.  145.   52.   72.  165.   64.   32.   12.  214.
  234.  202.  129.  157.   56.   32.  117.  238.  250.  226.   93.  165.
   64.   20.  153.  242.  255.  230.  145.  129.   97.   52.  117.  226.
  246.  210.  117.  145.  190.   85.   36.  145.  178.  117.   20.  170.
  202.  153.   72.   32.   12.   52.   85.  194.  206.  190.  129.  117.
   85.  174.  182.  218.]
[6 4 9 6 5 7 8 6 6 7]


In [46]:
fig = plt.hist(flattenedimg, bins = 255)
plt.title('Original Histogram (Flat version)')
plt.show


Out[46]:
<function matplotlib.pyplot.show>

In [45]:
fig = plt.hist(flattenednewimg, bins = 255)
plt.title('Equalized Histogram (Flat version)')
plt.show


Out[45]:
<function matplotlib.pyplot.show>